package com.ruoyi.comment.controller;

import com.ruoyi.comment.domain.TbComment;
import com.ruoyi.comment.service.ITbCommentService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 评论Controller
 * 
 * @author 劳雅玲
 * @date 2024-03-27
 */
@RestController
@RequestMapping("/comment/comment")
public class TbCommentController extends BaseController
{
    @Autowired
    private ITbCommentService tbCommentService;

    /**
     * 查询评论列表
     */
    @PreAuthorize("@ss.hasPermi('comment:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbComment tbComment)
    {
        startPage();
        List<TbComment> list = tbCommentService.selectTbCommentList(tbComment);
        return getDataTable(list);
    }

    /**
     * 查询评论列表-树状结构
     */
//    @PreAuthorize("@ss.hasPermi('comment:comment:list')")
    @GetMapping("/listTree")
    public TableDataInfo listTree(TbComment tbComment)
    {
        startPage();
        List<TbComment> list = tbCommentService.listTree(tbComment);
        return getDataTable(list);
    }

    /**
     * 导出评论列表
     */
    @PreAuthorize("@ss.hasPermi('comment:comment:export')")
    @Log(title = "评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbComment tbComment)
    {
        List<TbComment> list = tbCommentService.selectTbCommentList(tbComment);
        ExcelUtil<TbComment> util = new ExcelUtil<TbComment>(TbComment.class);
        util.exportExcel(response, list, "评论数据");
    }

    /**
     * 获取评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('comment:comment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(tbCommentService.selectTbCommentById(id));
    }

    /**
     * 新增评论
     */
    @PreAuthorize("@ss.hasPermi('comment:comment:add')")
    @Log(title = "评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbComment tbComment)
    {
        return toAjax(tbCommentService.insertTbComment(tbComment));
    }

    /**
     * 修改评论
     */
    @PreAuthorize("@ss.hasPermi('comment:comment:edit')")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbComment tbComment)
    {
        return toAjax(tbCommentService.updateTbComment(tbComment));
    }

    /**
     * 删除评论
     */
    @PreAuthorize("@ss.hasPermi('comment:comment:remove')")
    @Log(title = "评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(tbCommentService.deleteTbCommentByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('comment:comment:edit')")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/ok/{id}")
    public AjaxResult dianzan(@PathVariable("id") Integer id)
    {
        tbCommentService.dianzan(id);
        return success("点赞成功");
    }
}
